<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\User;
use think\Db;

class Login extends Controller
{
	public function login()
	{
		return $this->fetch();
	}
	public function dologin(Request $request)
	{
		// $data = $request->param();
		$username = $request->param('username');
		$passwd = $request->param('passwd');
		$jm = md5($passwd);
		$data=array(
			'username' => $username,
			'passwd' => $jm,
		);
		$res = User::get($data);
		//$res = User::where('username',$username)->cache(true)->find();
		if($res){
			session('userInfo.id', $res->id);
			session('userInfo.name', $res->username);
			session('userInfo.email', $res->email);
			session('userInfo.admin', $res->admin);
			session('userInfo.status', true);
			if($res->admin == 1){
				$this->redirect('Admin/index',302);
				//$this->success("登录成功", 'admin/Admin/index');
			}else{
				$this->redirect('Index/index',302);
				//$this->success("登录成功", 'admin/Index/index');
			}
		} else {
			$this->error("账号或者密码错误！");
		}
	}
	public function logout()
	{
		session('userInfo',null);
		$this->redirect('Login/login',302);
		//$this->error('退出成功！',url('admin/Login/login'));
	}
}